<template>
	<view class="detail-container">
		<view v-if="loading" class="loading">
			<uni-load-more status="loading" :contentText="loadingText"></uni-load-more>
		</view>
		
		<block v-else>
			<view class="status-card">
				<view class="status-icon" :class="getStatusClass(applicationDetail.status)">
					<uni-icons :type="getStatusIcon(applicationDetail.status)" size="24" color="#fff"></uni-icons>
				</view>
				<view class="status-info">
					<view class="status-title">{{ applicationDetail.status }}</view>
					<view class="status-desc">{{ getStatusDesc(applicationDetail.status) }}</view>
				</view>
			</view>
			
			<view class="detail-card">
				<view class="card-title">基本信息</view>
				
				<view class="info-item">
					<text class="label">申请单元</text>
					<text class="value">{{ applicationDetail.unit_info_str || '暂无数据' }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">施工单位</text>
					<text class="value">{{ applicationDetail.construction_company || '暂无数据' }}</text>
				</view>
				
				<view class="info-item">
					<text class="label">预计工期</text>
					<text class="value">{{ applicationDetail.estimated_duration }} 天</text>
				</view>
				
				<view class="info-item">
					<text class="label">申请日期</text>
					<text class="value">{{ applicationDetail.application_date || '暂无数据' }}</text>
				</view>
				
				<view class="info-item" v-if="applicationDetail.approval_opinion">
					<text class="label">审批意见</text>
					<text class="value opinion">{{ applicationDetail.approval_opinion }}</text>
				</view>
			</view>
			
			<view class="detail-card" v-if="applicationDetail.design_plan_url && applicationDetail.design_plan_url.length > 0">
				<view class="card-title">设计方案</view>
				
				<view class="image-list">
					<view 
						class="image-item" 
						v-for="(item, index) in applicationDetail.design_plan_url" 
						:key="index"
						@click="previewImage(index)"
					>
						<image class="design-image" :src="item" mode="aspectFill"></image>
					</view>
				</view>
			</view>
			
			<view class="detail-card" v-if="applicationDetail.construction_staff_info && applicationDetail.construction_staff_info.length > 0">
				<view class="card-title">施工人员信息</view>
				
				<view 
					class="staff-item"
					v-for="(staff, index) in applicationDetail.construction_staff_info"
					:key="index"
				>
					<view class="staff-header">施工人员 #{{ index + 1 }}</view>
					<view class="staff-info">
						<view class="staff-row">
							<text class="staff-label">姓名</text>
							<text class="staff-value">{{ staff.name || '未填写' }}</text>
						</view>
						<view class="staff-row">
							<text class="staff-label">电话</text>
							<text class="staff-value">{{ staff.phone || '未填写' }}</text>
						</view>
						<view class="staff-row" v-if="staff.id_card">
							<text class="staff-label">身份证号</text>
							<text class="staff-value">{{ staff.id_card }}</text>
						</view>
					</view>
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	import { getDecorationApplicationDetail } from '@/api/services.js'
	
	export default {
		data() {
			return {
				applicationId: null,
				applicationDetail: {},
				loading: true,
				loadingText: {
					contentdown: '正在加载...',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				}
			}
		},
		onLoad(options) {
			if (options.id) {
				this.applicationId = options.id
				this.getApplicationDetail()
			} else {
				uni.showToast({
					title: '参数错误',
					icon: 'none'
				})
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}
		},
		methods: {
			// 获取装修备案详情
			async getApplicationDetail() {
				this.loading = true
				
				try {
					const response = await getDecorationApplicationDetail(this.applicationId)
					console.log('装修备案详情:', response)
					
					if (response.code === 200 && response.data) {
						this.applicationDetail = response.data
						
						// 处理设计方案URL和施工人员信息
						if (typeof this.applicationDetail.design_plan_url === 'string') {
							try {
								this.applicationDetail.design_plan_url = JSON.parse(this.applicationDetail.design_plan_url)
							} catch (e) {
								this.applicationDetail.design_plan_url = []
							}
						}
						
						if (typeof this.applicationDetail.construction_staff_info === 'string') {
							try {
								this.applicationDetail.construction_staff_info = JSON.parse(this.applicationDetail.construction_staff_info)
							} catch (e) {
								this.applicationDetail.construction_staff_info = []
							}
						}
					} else {
						uni.showToast({
							title: response.message || '获取详情失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('获取装修备案详情失败', error)
					
					uni.showToast({
						title: '获取详情失败，请重试',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			
			// 获取状态对应的样式类
			getStatusClass(status) {
				switch (status) {
					case '待审核':
					case '审核中':
						return 'status-pending'
					case '审核通过':
					case '施工中':
						return 'status-processing'
					case '已完工':
					case '已验收':
						return 'status-completed'
					case '审核驳回':
						return 'status-rejected'
					default:
						return 'status-default'
				}
			},
			
			// 获取状态对应的图标
			getStatusIcon(status) {
				switch (status) {
					case '待审核':
					case '审核中':
						return 'clock'
					case '审核通过':
					case '施工中':
						return 'checkmarkempty'
					case '已完工':
					case '已验收':
						return 'checkbox-filled'
					case '审核驳回':
						return 'closeempty'
					default:
						return 'info'
				}
			},
			
			// 获取状态对应的描述
			getStatusDesc(status) {
				switch (status) {
					case '待审核':
						return '您的申请已提交，等待管理员审核'
					case '审核中':
						return '管理员正在审核您的申请'
					case '审核通过':
						return '您的申请已通过审核，可以开始施工'
					case '施工中':
						return '装修工程正在进行中'
					case '已完工':
						return '装修工程已完成'
					case '已验收':
						return '装修工程已通过验收'
					case '审核驳回':
						return '您的申请未通过审核，请查看审批意见'
					default:
						return '状态未知'
				}
			},
			
			// 预览图片
			previewImage(current) {
				uni.previewImage({
					urls: this.applicationDetail.design_plan_url,
					current: current
				})
			}
		}
	}
</script>

<style lang="scss">
	.detail-container {
		padding: 20rpx;
		background-color: #F8F8F8;
		min-height: 100vh;
		
		.loading {
			padding: 60rpx 0;
			text-align: center;
		}
		
		.status-card {
			background-color: #FFFFFF;
			border-radius: 12rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
			
			.status-icon {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 30rpx;
				
				&.status-pending {
					background-color: #ff9900;
				}
				
				&.status-processing {
					background-color: #2979ff;
				}
				
				&.status-completed {
					background-color: #19be6b;
				}
				
				&.status-rejected {
					background-color: #dd524d;
				}
				
				&.status-default {
					background-color: #909399;
				}
			}
			
			.status-info {
				.status-title {
					font-size: 32rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
				}
				
				.status-desc {
					font-size: 26rpx;
					color: #666;
				}
			}
		}
		
		.detail-card {
			background-color: #FFFFFF;
			border-radius: 12rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
			
			.card-title {
				font-size: 30rpx;
				font-weight: bold;
				margin-bottom: 20rpx;
				position: relative;
				padding-left: 20rpx;
				
				&:before {
					content: '';
					position: absolute;
					left: 0;
					top: 50%;
					transform: translateY(-50%);
					width: 6rpx;
					height: 30rpx;
					background-color: #2979ff;
					border-radius: 3rpx;
				}
			}
			
			.info-item {
				display: flex;
				margin-bottom: 20rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.label {
					width: 150rpx;
					color: #666;
					font-size: 28rpx;
				}
				
				.value {
					flex: 1;
					font-size: 28rpx;
					
					&.opinion {
						color: #ff9900;
					}
				}
			}
			
			.image-list {
				display: flex;
				flex-wrap: wrap;
				margin: 0 -10rpx;
				
				.image-item {
					width: 200rpx;
					height: 200rpx;
					margin: 10rpx;
					border-radius: 8rpx;
					overflow: hidden;
					
					.design-image {
						width: 100%;
						height: 100%;
					}
				}
			}
			
			.staff-item {
				background-color: #f9f9f9;
				border-radius: 8rpx;
				padding: 20rpx;
				margin-bottom: 20rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.staff-header {
					font-size: 28rpx;
					font-weight: bold;
					margin-bottom: 15rpx;
				}
				
				.staff-info {
					.staff-row {
						display: flex;
						margin-bottom: 10rpx;
						
						&:last-child {
							margin-bottom: 0;
						}
						
						.staff-label {
							width: 120rpx;
							color: #666;
							font-size: 26rpx;
						}
						
						.staff-value {
							flex: 1;
							font-size: 26rpx;
						}
					}
				}
			}
		}
	}
</style> 